
let token = ''
let openid = ''


const request = async (method, url, data, header) => {
    console.log(method, url, data, header)
    // if(!token){
    //     const res = await get('/',{ name: '111' })
    //     token = res
    // }
    if(!openid) openid = wx.getStorageSync('openid') || ''
    return await new Promise((resolve, reject) => {
        wx.request({
            url, 
            method,
            data: {
                "current": data.current || 0,
                "data": {
                    ...data
                },
                "flowId": "",
                "pageSize": data.pageSize || 10,
                "sysId": "",
                "timestamp": "",
                "version": ""
            },
            timeout: 30000,
            header: {
              'content-type': 'application/json', // 默认值
              'openid': openid,
              ...header
            },
            success (res) {
                console.log(res)
                console.log('----------- success ----------', res)
                if(res.statusCode === 200){
                    if(res.data.code === '200'){
                        resolve(res.data.data)
                    }else{
                        reject({ errMsg: res.data.message })
                    }
                }else{
                    reject({ errMsg: res.errMsg })
                }
            },
            fail (err) {
                // console.log('----------- err ----------', err)
                reject({ errMsg: err.errMsg })
            },
        })
    }).catch(err => {
        console.log('----------- errerrerr ----------', err)
        throw new Error(err.errMsg)
    })
}

const get = (url, data, header) => request('GET', url, data || {}, header || {})
const post = (url, data, header) => request('POST', url, data || {}, header || {})

module.exports = {
    get,
    post
}
